package org.example.spring.service.userServiceImpl;

import org.example.spring.mapper.CategoryMapper;
import org.example.spring.mapper.FlowerMapper;
import org.example.spring.domain.pojo.Category;
import org.example.spring.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private FlowerMapper flowerMapper;

    @Override
    public ArrayList<Category> getAllCategory() {
        ArrayList<Category> categories = categoryMapper.getAllCategory();
        return categories;
    }

    @Override
    public boolean deleteCategory(String categoryName) {
        int count= flowerMapper.getCount(categoryName);
        if(count==0){
            categoryMapper.deleteCategory(categoryName);
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public boolean updateCategory(Category category) {

        int count= categoryMapper.getCount(category.getCategoryName());
        if(count!=0){
            return false;
        }

        String olderCategoryName = categoryMapper.getCategoryNameById(category.getCategoryId());
        //更新分类名称
        categoryMapper.updateCategory(category);

        //更新商品表中的分类名称
        flowerMapper.updateCategoryName(olderCategoryName, category.getCategoryName());

        return true;

    }

    @Override
    public boolean addCategory(String categoryName) {
        int count= categoryMapper.getCount(categoryName);
        if(count!=0){
            return false;
        }
        categoryMapper.addCategory(categoryName);
        return true;
    }
}
